package io.xccit.race.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import io.xccit.common.annotation.Log;
import io.xccit.common.core.controller.BaseController;
import io.xccit.common.core.domain.AjaxResult;
import io.xccit.common.enums.BusinessType;
import io.xccit.race.domain.RaceRegion;
import io.xccit.race.service.IRaceRegionService;
import io.xccit.common.utils.poi.ExcelUtil;
import io.xccit.common.core.page.TableDataInfo;

/**
 * 竞赛地区Controller
 * 
 * @author xccit
 * @date 2025-10-8
 */
@RestController
@RequestMapping("/race/region")
public class RaceRegionController extends BaseController
{
    @Autowired
    private IRaceRegionService raceRegionService;

    /**
     * 查询竞赛地区列表
     */
    @PreAuthorize("@ss.hasPermi('race:region:list')")
    @GetMapping("/list")
    public TableDataInfo list(RaceRegion raceRegion)
    {
        startPage();
        List<RaceRegion> list = raceRegionService.selectRaceRegionList(raceRegion);
        return getDataTable(list);
    }

    /**
     * 导出竞赛地区列表
     */
    @PreAuthorize("@ss.hasPermi('race:region:export')")
    @Log(title = "竞赛地区", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, RaceRegion raceRegion)
    {
        List<RaceRegion> list = raceRegionService.selectRaceRegionList(raceRegion);
        ExcelUtil<RaceRegion> util = new ExcelUtil<RaceRegion>(RaceRegion.class);
        util.exportExcel(response, list, "竞赛地区数据");
    }

    /**
     * 获取竞赛地区详细信息
     */
    @PreAuthorize("@ss.hasPermi('race:region:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(raceRegionService.selectRaceRegionById(id));
    }

    /**
     * 新增竞赛地区
     */
    @PreAuthorize("@ss.hasPermi('race:region:add')")
    @Log(title = "竞赛地区", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RaceRegion raceRegion)
    {
        return toAjax(raceRegionService.insertRaceRegion(raceRegion));
    }

    /**
     * 修改竞赛地区
     */
    @PreAuthorize("@ss.hasPermi('race:region:edit')")
    @Log(title = "竞赛地区", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RaceRegion raceRegion)
    {
        return toAjax(raceRegionService.updateRaceRegion(raceRegion));
    }

    /**
     * 删除竞赛地区
     */
    @PreAuthorize("@ss.hasPermi('race:region:remove')")
    @Log(title = "竞赛地区", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(raceRegionService.deleteRaceRegionByIds(ids));
    }

    /**
     * 查询所有省份
     */
    @GetMapping("/provinces")
    public AjaxResult getProvinces()
    {
        List<String> provinces = raceRegionService.selectAllProvinces();
        return success(provinces);
    }

    /**
     * 根据省份查询所有市
     */
    @GetMapping("/cities")
    public AjaxResult getCities(String province)
    {
        List<String> cities = raceRegionService.selectCitiesByProvince(province);
        return success(cities);
    }

    /**
     * 根据省份和市查询所有县/区
     */
    @GetMapping("/districts")
    public AjaxResult getDistricts(String province, String city)
    {
        List<String> districts = raceRegionService.selectDistrictsByProvinceAndCity(province, city);
        return success(districts);
    }
}
